package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"register/models"
)

type RegisterController struct {
	beego.Controller
}
type JsonMsg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *RegisterController) Show() {
	id, _ := c.GetInt("id")
	var register models.Register
	err := models.DB.Model(&models.Register{}).Where("id = ?", id).First(&register).Error
	if err != nil {
		c.Data["json"] = JsonMsg{
			Code: 500,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonMsg{
			Code: 200,
			Msg:  "查询成功",
			Data: register,
		}
		c.ServeJSON()
	}
}
func (c *RegisterController) List() {
	var register []models.Register
	err := models.DB.Model(&models.Register{}).Find(&register).Error
	if err != nil {
		c.Data["json"] = JsonMsg{
			Code: 500,
			Msg:  "展示失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonMsg{
			Code: 200,
			Msg:  "展示成功",
			Data: register,
		}
		c.ServeJSON()
	}
}
